package user;

import java.util.Arrays;

public class UserManager {

    User[] users = {};
    int size = 0;

    public boolean register(User user){
        //注册前要判断用户是否已经注册过
        int ifUserExist = ifUserExist(user.getUserName());
        //如果存在不能注册则返回false
        if (ifUserExist != -1){
            return false;
        }
        if (users.length == size){
            users = Arrays.copyOf(users,users.length + 5);
        }
        users[size] = user;
        size++;
        return true;
    }

    private int ifUserExist(String username) {
        for (int i = 0; i < size; i++) {
            if(username.equals(users[i].getUserName())){
                return i;
            }
        }
        return -1;
    }

    public boolean login(String name, String word){

        //1、先判断用户名是否存在
        int index = ifUserExist(name);
        if(index == -1){ //表示用户名不存在
            return false;
        }
        //进而比较密码
        if(word.equals( users[index].getPassWord())){
            return true;
        }
        return false;
    }

    public boolean isUserNull(){
        if (size == 0){
            return true;
        }
        return false;
    }

}
